﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class _Default : System.Web.UI.Page
{
    string strURL = "";
    string tutorclass = "";
    string tutortime = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        this.imgTutor.Visible = false;
        this.btnChange.Visible = false;
        lblImageValid.Visible = false;
    }
    protected void Submit_Click(object sender, EventArgs e)
    {
        string name = this.txtTutorName.Text.ToString();
        string address = this.txtTutorAdd.Text.ToString();
        DateTime birthday = DateTime.Parse(this.txtTutorBirth.Text.ToString());
        string description = this.txtTutorDes.Text.ToString() ;
        string email = this.txtTutorEmail.Text.ToString() ;
        string experience = this.txtTutorExper.Text.ToString() ;
        int idnumber = int.Parse(this.txtTutorIdNum.Text.ToString()) ;
        string mark = this.txtTutorMark.Text.ToString();
        string phone = this.txtTutorPhone.Text.ToString();
        string place = this.txtTutorPlace.Text.ToString();
        double salary = double.Parse(this.txtTutorSalary.Text.ToString());
        string specialize = this.txtTutorSpec.Text.ToString();
        string subject = this.txtTutorSub.Text.ToString();
        string university = this.txtTutorUni.Text.ToString() ;
        strURL = Server.MapPath(imgTutor.ImageUrl.ToString());        
        
        for (int i = 0; i < chkClass.Items.Count; i++ )
            if(chkClass.Items[i].Selected == true)
                 tutorclass += this.chkClass.Items[i].ToString()+ ";";

        for (int j = 0; j < chkTime.Items.Count; j++)
            if (chkTime.Items[j].Selected == true)
                tutortime += this.chkTime.Items[j].ToString() + ";";
        
        int education = int.Parse(this.drTutorEdu.SelectedValue.ToString());
        bool gender = bool.Parse(this.drTutorGend.SelectedValue.ToString());
        int job = int.Parse(this.drTutorJob.SelectedValue.ToString());
        int trans = int.Parse(this.drTutorTrans.SelectedValue.ToString());

        DCEdu.TutorProfile objProfile = new DCEdu.TutorProfile();
        if (objProfile.Add(name, gender, idnumber, address, birthday, phone, email, education, university, specialize, job, mark, strURL, subject, tutorclass, trans, experience, salary, place, tutortime, description) != -1)
        {
            ClientScript.RegisterStartupScript(this.GetType(), "ex", "<script>alert('Nhập liệu thành công')</script>");
            Page_Load(sender, e);
        }
        else
        {
            ClientScript.RegisterStartupScript(this.GetType(), "ex", "<script>alert('Lỗi nhập liệu')</script>");
            DelImage();
        }
            

}

    protected void Reset_Click(object sender, EventArgs e)
    {
        this.txtTutorName.Text = "";
        this.txtTutorAdd.Text = "";
        this.txtTutorBirth.Text = "";
        this.txtTutorDes.Text = "";
        this.txtTutorEmail.Text = "";
        this.txtTutorExper.Text = "";
        this.txtTutorIdNum.Text = "";
        this.txtTutorMark.Text = "";
        this.txtTutorPhone.Text = "";
        this.txtTutorPlace.Text = "";
        this.txtTutorSalary.Text = "";
        this.txtTutorSpec.Text = "";
        this.txtTutorSub.Text = "";
        this.txtTutorUni.Text = "";
        this.chkClass.SelectedIndex = -1;
        this.chkTime.SelectedIndex = -1;
        this.drTutorEdu.SelectedIndex = 0;
        this.drTutorGend.SelectedIndex = 0;
        this.drTutorJob.SelectedIndex = 0;
        this.drTutorTrans.SelectedIndex = 0;
        DelImage();
    }

    protected void btnUpload_Click(object sender, EventArgs e)
    {
        if (this.FileUpload1.FileName.Equals(""))
        {
            lblImageValid.Visible = true;
            this.lblImageValid.Text = "Chưa chọn ảnh";
        }
        else
        {
            strURL = Server.MapPath("/TutorImages/" + FileUpload1.FileName);
            string name_image = FileUpload1.FileName.ToString();
            if (name_image.EndsWith(".jpg") || name_image.EndsWith(".jpeg") || name_image.EndsWith(".gif") || name_image.EndsWith(".bmp") || name_image.EndsWith(".png"))
            {
                imgTutor.ImageUrl = "/TutorImages/" + name_image;
                FileUpload1.SaveAs(strURL);
                imgTutor.Visible = true;
                btnUpload.Visible = false;
                FileUpload1.Visible = false;
                btnChange.Visible = true;
                this.lblImageValid.Text = "";
                imgTutor.AlternateText = name_image;
            }
            else
                this.lblImageValid.Text = "Sai định dạng file";
        }
    }

    protected void btnChange_Click(object sender, EventArgs e)
    {
        btnUpload.Visible = true;
        FileUpload1.Visible = true;
        btnChange.Visible = false;
        DelImage();
    }

    private void DelImage() 
    {
        strURL = Server.MapPath(imgTutor.ImageUrl.ToString());
        System.IO.File.Delete(strURL);
    }
}
